package com.example.demo.controller;

import com.example.demo.entity.Address;
import com.example.demo.entity.Goods;
import com.example.demo.entity.Result;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.HtmlUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@CrossOrigin
@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @CrossOrigin
    @PostMapping(value = "/api/login")
    @ResponseBody
    public Result login(@RequestBody User requestUser)
    {
        System.out.println(requestUser.toString());
        String phone = requestUser.getPhone();
        System.out.println(phone);
        String password = requestUser.getPassword();
        System.out.println(password);
        User user = userService.selectUserByPhoneAndPassword(phone,password);

        if(user == null || !user.getPassword().equals(password))
        {
            Map<String,Object> map = new HashMap<String,Object>();
            String message = "账号密码错误";
            Result result = new Result(400);
            map.put("userid",null);
            map.put("username",null);
            result.setData(new User());
            return result;
        }
        else
        {
            Map<String,Object> map = new HashMap<String,Object>();
            Result result = new Result(200);
            map.put("userid",user.getUserid());
            map.put("phone",user.getPhone());
            user.setPassword(null);
            result.setData(user);
            return result;
        }
    }


    @CrossOrigin
    @PostMapping(value = "/api/admin/login")
    @ResponseBody
    public Result adminLogin(@RequestBody User requestUser)
    {
        System.out.println(requestUser.toString());
        String phone = requestUser.getPhone();
        System.out.println(phone);
        String password = requestUser.getPassword();
        System.out.println(password);
        User user = userService.selectUserByPhoneAndPassword(phone,password);

        if(user == null || !user.getPassword().equals(password) || user.getIsAdmin()!=1)
        {
            Map<String,Object> map = new HashMap<String,Object>();
            String message = "账号密码错误";
            Result result = new Result(400);
            map.put("userid",null);
            map.put("username",null);
            result.setData(new User());
            return result;
        }
        else
        {
            Map<String,Object> map = new HashMap<String,Object>();
            Result result = new Result(200);
            map.put("userid",user.getUserid());
            map.put("phone",user.getPhone());
            user.setPassword(null);
            result.setData(user);
            return result;
        }
    }

    @CrossOrigin
    @PostMapping(value = "/api/register")
    @ResponseBody
    public Result register(@RequestBody User requestUser)
    {

        User temp = new User();
        temp.setPhone(requestUser.getPhone());
        Result result = new Result(200);
        if(checkexit(temp).getCode()==200)
        {

            result.setCode(400);
            return result;
        }

        String username = requestUser.getUsername();
        username = HtmlUtils.htmlEscape(username);
        String password = requestUser.getPassword();
        String phone = requestUser.getPhone();
        userService.insertnewUser(username,password,phone);
        User user = userService.findByPhone(phone);
        user.setPassword(null);
        result.setData(user);
        result.setCode(200);
        return result;
    }

    @CrossOrigin
    @PostMapping(value = "/api/checkexit")
    @ResponseBody
    public Result checkexit(@RequestBody User requestUser)
    {
        String phone = requestUser.getPhone();
        System.out.println(phone);
        User user = userService.findByPhone(phone);

        if(user == null)
        {
            String message = "账号不存在";
            Result result = new Result(400);
            result.setData(null);
            return result;
        }
        else
        {
            Map<String,Object> map = new HashMap<String,Object>();
            Result result = new Result(200);
            result.setData(map);
            return result;
        }

    }

    @CrossOrigin
    @PostMapping(value = "/api/checkexitbytoken")
    @ResponseBody
    public Result checkexitbytoken(@RequestBody User requestUser)
    {
        String token = requestUser.getToken();

        User user = userService.findByToken(token);
        System.out.println(user.toString());
        if(user == null)
        {
            String message = "账号不存在";
            Result result = new Result(400);
            result.setData(null);

            return result;
        }
        else
        {
            Map<String,Object> map = new HashMap<String,Object>();
            Result result = new Result(200);
            user.setPassword(null);
            result.setData(user);
            return result;
        }

    }

    @CrossOrigin
    @PostMapping(value = "/api/findbyid")
    @ResponseBody
    public Result findbyid(@RequestBody User requestUser)
    {
        int userid = requestUser.getUserid();
        User user = userService.findById(userid);
        Result result = new Result(200);
        result.setData(user);
        return result;
    }

    @CrossOrigin
    @PostMapping(value = "/api/updatebyid")
    @ResponseBody
    public Result updatebyid(@RequestBody User requestUser)
    {

        userService.updateById(requestUser);

        Result result = new Result(200);

        return result;
    }

    @CrossOrigin
    @PostMapping(value = "/api/updateVIP")
    @ResponseBody
    public Result updateVIP(@RequestBody User requestUser)
    {
        System.out.println(requestUser.toString());
        userService.updateVIP(requestUser);
        Result result = new Result(200);

        return result;
    }

    @CrossOrigin
    @PostMapping(value = "/api/bindUser")
    @ResponseBody
    public Result bindUser(@RequestBody User requestUser)
    {
        System.out.println(requestUser.getToken());
        userService.bindUser(requestUser.getUserid(),requestUser.getToken());
        Result result = new Result(200);

        return result;
    }

    @CrossOrigin
    @PostMapping(value = "/api/addMoney")
    @ResponseBody
    public Result addMoney(@RequestBody User requestUser)
    {
        User user = userService.findById(requestUser.getUserid());
        userService.addMoney(requestUser.getUserid(),user.getPoint()+2000);
        user = userService.findById(requestUser.getUserid());
        Result result = new Result(200);
        result.setData(user.getPoint());
        return result;
    }



    @CrossOrigin
    @PostMapping(value = "/api/listAddress")
    @ResponseBody
    public Result listAddress(@RequestBody User requestUser)
    {
        List<Address> addresses =  userService.getAddress(requestUser.getUserid());
        Result result = new Result(200);
        result.setData(addresses);
        return result;
    }


    @CrossOrigin
    @PostMapping(value = "/api/addAddress")
    @ResponseBody
    public Result addAddress(@RequestBody Address requestAddress)
    {

        userService.insertnewAddress(requestAddress);
        System.out.println(requestAddress.getAddressid());
        Result result = new Result(200);

        return result;
    }

    @CrossOrigin
    @PostMapping(value = "/api/updateAddress")
    @ResponseBody
    public Result updateAddress(@RequestBody Address requestAddress)
    {

        userService.updateAddress(requestAddress.getAddress(),requestAddress.getPhone(),requestAddress.getName(),requestAddress.getAddressid(),requestAddress.getRegion());

        Result result = new Result(200);
        return result;
    }

    @CrossOrigin
    @PostMapping(value = "/api/deleteAddress")
    @ResponseBody
    public Result deleteAddress(@RequestBody Address requestAddress)
    {

        userService.deleteAddress(requestAddress.getAddressid());

        Result result = new Result(200);


        return result;
    }

    @CrossOrigin
    @PostMapping(value = "/api/findAddressById")
    @ResponseBody
    public Result findAddressById(@RequestBody Address requestAddress)
    {
        Result result = new Result(200);
        result.setData(userService.findAddressById(requestAddress.getAddressid()));
        return result;
    }

    @CrossOrigin
    @GetMapping(value = "/api/getUserList")
    @ResponseBody
    public Result getUserList(@RequestParam("currentPage") int currentPage,
                                  @RequestParam("searchMsg") String searchMsg){
        List<User> users = userService.getUserList(currentPage,searchMsg);
        int total = userService.getUserListTotal(searchMsg);
        Result result = new Result(200);
        result.setData(users);
        result.setTotal(total);
        return result;
    }

    @GetMapping(value = "/api/deleteUser")
    @ResponseBody
    public void deleteUserByID(@RequestParam("id") int id){
        userService.deleteUserById(id);
    }

    @PostMapping(value = "/api/editUser")
    @ResponseBody
    public void editUser(@RequestBody User user){
        System.out.println(user);
        userService.editUser(user);
    }



}

